<?php
/**
 * LogStream
 *
 * 日志存文件
 * 需要将文件地址做为参数赋给构造方法
 * 
 * @author   songhuan <iphper@yeah.net>
 * @version   $Id: LogStream.php 1 2010-08-10 14:40:00Z $
 */
class LogStream extends Log
{
	/**
     * @var null|stream 文件指针
     */
    protected $_stream = null;
	
    public function __construct($path)
    {
    	if (!$this->_stream = @fopen($path, 'a', false))
    		throw new Exception("'$path' cannot be opened with mode 'a'");
    } 

    /**
     * 关闭文件指针
     *
     * @return void
     */
    public function shutdown()
    {
        if (is_resource($this->_stream)) 
            fclose($this->_stream);
    }

    /**
     * 向文件中写一条日志
     *
     * @param  array  $event  event data
     * @return void
     */
    protected function _write($event)
    {
        $line = $this->_format($event);

        if (false === @fwrite($this->_stream, $line)) 
            throw new Exception('Unable to write to stream');
    }
}
